// pages/refund_form/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['申通快递', '京东快递', '菜鸟快递', '顺丰快递'],
    img: [],
    logistics: ''
  },

  //选择物流公司
  bindPickerChange: function (e) {
    this.setData({
      logistics: this.data.array[e.detail.value]
    })
  },

  //扫码
  scanCode: function () {
    console.log('进入扫码')
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        this.setData({
          number: res.result
        })
        console.log(res)
      }
    })
  },
  // 添加图片
  addimg: function () {
    let that = this;
    var img = that.data.img;
    console.log(img, 'img')
    console.log(img.length)
    if (img.length >= 3) {
      wx.showToast({
        title: '图片添加数量超出',
        duration: 1000
      })
    } else {
      //console.log('已添加' + img.length + 1)
      var imglength = 2 - img.length
      wx.chooseImage({
        count: imglength + 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          //console.log(res.tempFilePaths)
          var list = res.tempFilePaths
          for (var i = 0; i < list.length; i++) {
            wx.uploadFile({
              url: app.data.Url + 'spell/upload',
              filePath: list[i],
              name: 'image',
              header: { "Content-Type": "multipart/form-data" },
              success: function (e) {
                console.log(JSON.parse(e.data).img)
                img.push(JSON.parse(e.data).img)
                that.setData({
                  img: img
                })
              }
            })
          }
          
          console.log(that.data.img)
        }
      })
    }

  },
  //删除图片
  delimg: function (e) {
    var img = this.data.img;
    img.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      img: img
    })
  },

  //表单提交
  formSubmit:function(e){
    let that = this;
    var phone = e.detail.value.phone;      //手机号
    var reason = e.detail.value.reason;    //原因
    var imgs = ''                           //图片集
    if (phone == ''){
      wx.showToast({
        title: '请填写手机号',
        icon: 'none'
      })
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(phone))){
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return false;
    }
    if (reason == '') {
      wx.showToast({
        title: '请填写退货原因',
        icon: 'none'
      })
      return false;
    }
    var imgurl = that.data.img
    wx.request({
      url: 'https://zhapp.4hl.cn/app/order/tuihuo',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        orderid: that.data.orderid,
        phone: phone,
        content: reason,
        imgs: imgurl.join(',')
      },
      method: 'post',
      success: function (res) {
        if(res.data.data.code == 1){
          wx.navigateTo({
            url: '../refunddetails/index?id=' + that.data.orderid
          })
        }
      }
    })
    console.log(e)

  },

  //提交表单
  onform: function (e) {
    console.log(e.detail.value)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderid: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})